package vn.student.controller;

import java.util.ArrayList;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.ViewClassDetail;
import vn.student.mylib.ClassInfoST;
import vn.student.mylib.Constants;
import vn.student.mylib.ListClassSTAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewHomeController{
	Activity act = null;
	AStudentDB myDB = null;
	public ViewHomeController(Activity act){
		this.act = act;
		myDB = new AStudentDB(this.act);
		//this.onClickViewTodayClass();
	}
	private void getLayout(){
		LinearLayout lnl = (LinearLayout)act.findViewById(R.id.lnl_main);
		lnl.removeAllViews();  
        LayoutInflater inflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.home, lnl);
		RelativeLayout viewInstructorContact = (RelativeLayout) act.findViewById(R.id.viewTodayClassBtn);
		viewInstructorContact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onClickViewTodayClass();
			}
		});
		RelativeLayout viewStudentContact = (RelativeLayout) act.findViewById(R.id.viewNewsBtn);
		viewStudentContact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//onClickViewNews();
			}
		});
	}
	public void onClickViewTodayClass(){
		ListView listView = (ListView) act.findViewById(R.id.list_view);
		ArrayList<ClassInfoST> list = new ArrayList<ClassInfoST>();
		list = myDB.getTodayClassList();
		ArrayList<ClassInfoST> listCurrentClass = new ArrayList<ClassInfoST>();
		if(list==null||list.size()==0){
			LinearLayout lnl = (LinearLayout)act.findViewById(R.id.lnl_main);
			lnl.removeAllViews();
	        LayoutInflater inflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			inflater.inflate(R.layout.no_class, lnl);
			((TextView)act.findViewById(R.id.noClassLabel)).setText("Today have no class. Enjoin it;)");
			return;
		}
		else{
			list = this.getNowClass(list);
			ClassInfoST tmp = null;
			for(;list.size()>0;){
				if(this.isCurrentClass(list.get(0))){
					tmp = list.remove(0);
					listCurrentClass.add(tmp);
				}
				else 
					break;
			}
			if(list.size()==0&&listCurrentClass.size()==0){
				LinearLayout lnl = (LinearLayout)act.findViewById(R.id.lnl_main);
				lnl.removeAllViews();
		        LayoutInflater inflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				inflater.inflate(R.layout.no_class, lnl);
				return;
			}
			else{
				getLayout();
			}
		}
		if(listCurrentClass!=null&&listCurrentClass.size()>0){
			LinearLayout view = (LinearLayout) act.findViewById(R.id.lnl_main);
			((TextView)act.findViewById(R.id.nowLabel)).setVisibility(View.VISIBLE);
			((ListView)act.findViewById(R.id.now_list_view)).setVisibility(View.VISIBLE);
			ListView listView1 = (ListView) act.findViewById(R.id.now_list_view);
			listView1.removeAllViewsInLayout();
			listView1.setAdapter(new ListClassSTAdapter(act,listCurrentClass));
			listView1.getLayoutParams().height = listCurrentClass.size()*95;
			listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(act,ViewClassDetail.class);
					Bundle bundle = new Bundle();
					ListClassSTAdapter adapter = (ListClassSTAdapter) arg0.getAdapter();
					ArrayList<ClassInfoST> list = adapter.getClassInfoList();
					bundle.putString("id", list.get(arg2).id);
					bundle.putBoolean("isViewStudentAss", true);
					intent.putExtras(bundle);
					act.startActivityForResult(intent, Constants.ViewClassDetailRequestCode);
				}
			});
		}else{ 
			LinearLayout view = (LinearLayout) act.findViewById(R.id.lnl_main);
			((TextView)act.findViewById(R.id.nowLabel)).setVisibility(View.INVISIBLE);
			((ListView)act.findViewById(R.id.now_list_view)).setVisibility(View.INVISIBLE);
		}
		if(list!=null&&list.size()>0){
			LinearLayout view = (LinearLayout) act.findViewById(R.id.lnl_main);
			((TextView)act.findViewById(R.id.nextLabel)).setVisibility(View.VISIBLE);
			listView = ((ListView)act.findViewById(R.id.list_view));
			((ListView)act.findViewById(R.id.list_view)).setVisibility(View.VISIBLE);
			listView.setAdapter(new ListClassSTAdapter(act,list)); 
			listView.getLayoutParams().height = list.size()*95; 
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(act,ViewClassDetail.class);
					Bundle bundle = new Bundle();
					ListClassSTAdapter adapter = (ListClassSTAdapter) arg0.getAdapter();
					ArrayList<ClassInfoST> list = adapter.getClassInfoList();
					bundle.putString("id", list.get(arg2).id);
					bundle.putBoolean("isViewStudentAss", true);
					intent.putExtras(bundle);
					act.startActivityForResult(intent, Constants.ViewClassDetailRequestCode);
				}
			});

		}else{
			LinearLayout view = (LinearLayout) act.findViewById(R.id.lnl_main);
			((TextView)act.findViewById(R.id.nextLabel)).setVisibility(View.INVISIBLE);
			((ListView)act.findViewById(R.id.list_view)).setVisibility(View.INVISIBLE);
		}
	}
	public void onClickViewNews(){
		LayoutInflater inflater=act.getLayoutInflater();
		RelativeLayout tmp = (RelativeLayout) act.findViewById(R.id.widgetHome);
		tmp.removeAllViews();
   	 	inflater.inflate(R.layout.home_nguyen, tmp);
	}
	public ArrayList<ClassInfoST> getNowClass(ArrayList<ClassInfoST> list){
		for(int i=0;i<list.size();i++){
			ClassInfoST classInfo = list.get(i);
			String[] timeStart = classInfo.timeStart.split(":");
			String[] timeEnd = classInfo.timeEnd.split(":");
			Time tmp1 = new Time();
			tmp1.setToNow();
			tmp1.hour = Integer.parseInt(timeStart[0]);
			tmp1.minute = Integer.parseInt(timeStart[1]);
			tmp1.second = 0;
			
			Time tmp2 = new Time();
			tmp2.setToNow();
			tmp2.hour = Integer.parseInt(timeEnd[0]);
			tmp2.minute = Integer.parseInt(timeEnd[1]);
			tmp2.second = 0;
			
			Time tmp3 = new Time();
			tmp3.setToNow();
			if(Time.compare(tmp1, tmp3)<0 && Time.compare(tmp2, tmp3)<0){
				list.remove(i);
				i--;
			}
			if(Time.compare(tmp1, tmp3)<=0 && Time.compare(tmp2, tmp3)>=0){
				return list;
			}
		}
		return list;
	}
	private boolean isCurrentClass(ClassInfoST classInfo){
		String[] timeStart = classInfo.timeStart.split(":");
		String[] timeEnd = classInfo.timeEnd.split(":");
		Time tmp1 = new Time();
		tmp1.setToNow();
		tmp1.hour = Integer.parseInt(timeStart[0]);
		tmp1.minute = Integer.parseInt(timeStart[1]);
		tmp1.second = 0;
		
		Time tmp2 = new Time();
		tmp2.setToNow();
		tmp2.hour = Integer.parseInt(timeEnd[0]);
		tmp2.minute = Integer.parseInt(timeEnd[1]);
		tmp2.second = 0;
		
		Time tmp3 = new Time();
		tmp3.setToNow();
		
		if(Time.compare(tmp1, tmp3)<=0 && Time.compare(tmp2, tmp3)>=0)
			return true;
		else 
			return false;
	}
	private boolean isLeastClass(ClassInfoST classInfo){
		String[] timeStart = classInfo.timeStart.split(":");
		String[] timeEnd = classInfo.timeEnd.split(":");
		Time tmp1 = new Time();
		tmp1.setToNow();
		tmp1.hour = Integer.parseInt(timeStart[0]);
		tmp1.minute = Integer.parseInt(timeStart[1]);
		tmp1.second = 0;
		
		Time tmp2 = new Time();
		tmp2.setToNow();
		tmp2.hour = Integer.parseInt(timeEnd[0]);
		tmp2.minute = Integer.parseInt(timeEnd[1]);
		tmp2.second = 0;
		
		Time tmp3 = new Time();
		tmp3.setToNow();
		
		if(Time.compare(tmp1, tmp3)<0 && Time.compare(tmp2, tmp3)<0)
			return true;
		else 
			return false;
	}
}
